<?php
class EditAdminNoticeUserForm extends Form{

	function EditAdminNoticeUserForm(){		
		Form::Form('EditAdminNoticeUserForm');
		CGlobal::$website_title="Gửi thông báo cho thành viên";		
		$this->link_css('style/enbacStyle.css');								
	}
	
	function on_submit(){
	
		$user_name = AZLib::getParam('user_name');
		$content = AZLib::getParam('content');
		$active = (int)Url::get('active');
		$expire = (int)Url::get('expire',7);
		$id = (int)Url::get('id');
		$cmd = Url::get('cmd');
				
		$sql = "SELECT id FROM user WHERE user_name='$user_name'";
		$row = DB::fetch($sql);
		$user_id = (int)$row["id"];		
		
		if(!$user_id){
			$this->setFormError('user_name','Không tồn tại thành viên này!'); 
		}								
		elseif(!$user_name || !$content){
			$this->setFormError('content','Dữ liệu không hợp lệ!'); 
		}
		else{				
			if($cmd=="add"){
				$item_array = array(
							'content' => $content,
							'user_name' => $user_name,
							'user_id' => $user_id,
							'admin_add' => User::user_name(),
							'active' => $active,
							'time_add' => TIME_NOW,
							'expire_date' => TIME_NOW + 86400*$expire,
					);
					
				DB::insert('admin_notice_user',	$item_array);
			}
			elseif($cmd=="edit" && $id){
				$item_array = array(
							'content' => $content,
							'user_name' => $user_name,
							'user_id' => $user_id,
							'admin_edit' => User::user_name(),
							'active' => $active,
							'time_edit' => TIME_NOW,
							'expire_date' => TIME_NOW + 86400*$expire,
					);
				DB::update_id('admin_notice_user', $item_array,$id);	
			}
			User::getAdminNoticeUser($user_id,0,1);
			Url::redirect_current(array('act'));
		}
		
	}
	
	function draw(){	
		$this->beginForm();		
		global $display;		
				
		$id  = (int)Url::get("id",0);
		$cmd = Url::get("cmd");
		
		if($id && $cmd=="edit"){
			$item = DB::fetch("SELECT * FROM admin_notice_user WHERE id=$id");
			$display->add('user_name', $item['user_name']);
			$display->add('content', $item['content']);
			
			$expire = (int)round(($item['expire_date'] - TIME_NOW)/86400);
			$display->add('expire', $expire);
			
			if($item["expire_date"]>TIME_NOW && $item["active"]){
				$item["active"] = 1;	
			}
			else{
				$item["active"] = 0;	
			}
			
			$display->add('active', $item['active']);
		}
		elseif($cmd=="add"){
			$display->add('user_name', Url::get('user_name'));
			$display->add('content', Url::get('content'));			
			$display->add('active', (int)Url::get('active',1));
			$display->add('expire', Url::get('expire',7));
		}
		$display->add('cmd', $cmd);		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->output('EditAdminNoticeUser');		
		$this->endForm();
	}
}
?>